# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=libunwind
pkgbase=mingw-w64-${_realname}-svn
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-svn"
pkgver=17.6127858
pkgrel=1
pkgdesc='A new implementation of a stack unwinder for C++ exceptions'
url='https://llvm.org/'
license=('custom:University of Illinois/NCSA Open Source License')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-llvm")
source=("libunwind"::"git+https://llvm.org/git/libunwind.git"
        0001-libunwind-add-support-for-mingw-w64.patch)

sha256sums=('SKIP'
            'c05162c426e90a01cef4a9d34d09e6314b966f2fea5a121223c98e51f5b6bd93')

pkgver_internal() {
  if [ ! -d "$1" ]; then
    plain "No such directory $1"
    exit 1
  fi
  pushd "$1" > /dev/null 2>&1
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  popd > /dev/null 2>&1
}

pkgver() {
  pkgver_internal "${srcdir}"/libunwind
}

prepare() {
  cd ${srcdir}/libunwind
  git am "${srcdir}/"0001-libunwind-add-support-for-mingw-w64.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DLLVM_CONFIG="${MINGW_PREFIX}/bin/llvm-config.exe" \
    -DCMAKE_C_COMPILER="${MINGW_PREFIX}/bin/gcc.exe" \
    -DCMAKE_CXX_COMPILER="${MINGW_PREFIX}/bin/g++.exe" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DLIBUNWIND_ENABLE_SHARED=OFF \
    ../libunwind
  make

}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="$pkgdir" install
}
